/*
 * License GNU LGPL
 * Copyright (C) 2012 Amrullah <amrullah@panemu.com>.
 */
package com.abc.cheque.ui;

import java.io.IOException;
import java.util.List;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.layout.VBox;

import com.abc.cheque.common.TableData;
import com.abc.cheque.common.TiwulFXUtil;
import com.abc.cheque.control.LookupFieldController;
import com.abc.cheque.entity.Contact;
import com.abc.cheque.test.TiwulfxDemo;
import com.abc.cheque.ui.dialog.MessageDialogBuilder;
import com.abc.cheque.ui.table.CheckBoxColumn;
import com.abc.cheque.ui.table.ComboBoxColumn;
import com.abc.cheque.ui.table.DateColumn;
import com.abc.cheque.ui.table.LookupColumn;
import com.abc.cheque.ui.table.NumberColumn;
import com.abc.cheque.ui.table.TableControl;
import com.abc.cheque.ui.table.TableController;
import com.abc.cheque.ui.table.TextColumn;
import com.abc.cheque.ui.table.TickColumn;


/**
 *
 * @author Amrullah <amrullah@panemu.com>
 */
public class FrmContactTable extends VBox {

    @FXML
    protected TableControl<Contact> tblContact;
   // private DaoBase<Insurance> daoInsurance = new DaoBase<>(Insurance.class);

    public FrmContactTable(TableController<Contact> controller) {
        FXMLLoader fxmlLoader = new FXMLLoader(FrmContactTable.class.getResource("FrmPersonTable.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
        	
            throw new RuntimeException(exception);
        }
        this.controller = controller;
       // init();
        tblContact.reloadFirstPage();
    }

   protected void init() {
        tblContact.setRecordClass(Contact.class);
        tblContact.setController(controller);
        tblContact.setMaxRecord(50);
        tblContact.getTableView().setTableMenuButtonVisible(true);
        TiwulfxDemo.factory.createEntityManager();
        TextColumn<Contact> clmName = new TextColumn<>(Person_.name.getName(), 150);
        clmName.setEditable(true);
        TextColumn<Contact> clmEmail = new TextColumn<>(Person_.email.getName(), 250);
        DateColumn<Contact> clmBirthDate = new DateColumn<>(Person_.birthDate.getName());
        clmBirthDate.setRequired(false);
        ComboBoxColumn<Contact, String> clmBirthPlace = new ComboBoxColumn<>(Person_.birthPlace.getName());
        for (String location : DataGenerator.birthPlaces) {
            clmBirthPlace.addItem(location, location);
        }

        ComboBoxColumn<Contact, Character> clmGender = new ComboBoxColumn<>(Person_.gender.getName());
        clmGender.addItem("Male", 'm');
        clmGender.addItem("Female", 'f');
        clmGender.setRequired(true);

        CheckBoxColumn<Contact> clmAlive = new CheckBoxColumn<>(Person_.alive.getName());
        clmAlive.setRequired(false);
        clmAlive.setLabel(TiwulFXUtil.getLiteral("alive.true"), TiwulFXUtil.getLiteral("alive.false"), TiwulFXUtil.getLiteral("alive.null"));
        
        NumberColumn<Contact, Integer> clmInsuranceId = new NumberColumn<>(Person_.insurance.getName() + "." + Insurance_.id.getName(), Integer.class);
        clmInsuranceId.setEditable(false);
        
        LookupColumn<Contact, Insurance> clmInsurance = new LookupColumn<>(Person_.insurance.getName(), Insurance_.code.getName(), 75);
        clmInsurance.setLookupController(insuranceLookupController);
        clmInsurance.setText(TiwulFXUtil.getLiteral("insurance.code"));
        
        TextColumn<Contact> clmInsuranceName = new TextColumn<>(Person_.insurance.getName() + "." + Insurance_.name.getName(), 200);
        clmInsuranceName.setEditable(false);
        TableColumn clmInsuranceDesc = new TableColumn("Description");
        clmInsuranceDesc.getColumns().addAll(clmInsurance, clmInsuranceName);
        
        TableColumn clmInsuranceHeader = new TableColumn("Insurance");
        clmInsuranceHeader.getColumns().addAll(clmInsuranceId, clmInsuranceDesc);
        
        NumberColumn<Contact, Integer> clmVisit = new NumberColumn<>(Person_.visit.getName(), Integer.class, 75);

       
       // Custom column. Not included in TiwulFX library
         
        ProgressBarColumn<Contact, Integer> clmProgress = new ProgressBarColumn<>(Person_.visit.getName());
        clmProgress.setEditable(false);
        clmProgress.setMax(5000);
        
        NumberColumn<Contact, Double> clmWeight = new NumberColumn<>(Person_.weight.getName(), Double.class, 75);

        NumberColumn<Contact, Integer> clmVersion = new NumberColumn<>(Person_.version.getName(), Integer.class, 50);
        clmVersion.setEditable(false);

        
        final TickColumn<Contact> clmTick = new TickColumn<>();
        clmTick.setDefaultTicked(true);
        tblContact.addColumn(clmTick, clmName, clmEmail, clmBirthPlace, clmBirthDate,
                clmGender, clmAlive,
                clmInsuranceHeader,
                clmVisit, clmProgress, clmWeight, clmVersion);
        tblContact.setAgileEditing(true);
        Button btnGetTicked = new Button("Get Ticked Records");
        btnGetTicked.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                MessageDialogBuilder.info().title("Ticked Records")
                        .message(clmTick.getTickedRecords().size() + " ticked\n" + clmTick.getUntickedRecords().size() + " unticked")
                        .show(getScene().getWindow());
            }
        });
        tblContact.addButton(btnGetTicked);
    }
    private TableController<Contact> controller;
    private LookupFieldController<Insurance> insuranceLookupController = new LookupFieldController<Insurance>(Insurance.class) {
        @Override
        public String[] getColumns() {
            return new String[]{
                        Insurance_.id.getName(),
                        Insurance_.code.getName(),
                        Insurance_.name.getName()
                    };
        }

        @Override
        protected String getWindowTitle() {
            return "Find Insurance";
        }
        
        @Override
        protected void initCallback(VBox container, TableControl<Insurance> table) {
            container.setPrefWidth(500);
            table.getTableView().getColumns().get(2).setPrefWidth(300);
        }

        @Override
        protected TableData loadData(int startIndex, List filteredColumns, List sortedColumns, List sortingTypes, int maxResult) {
            return daoInsurance.fetch(startIndex, filteredColumns, sortedColumns, sortingTypes, maxResult);
        }
    };
}
